#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>

/*
 * for nice() is used to increase the nice value of the calling process,so this tool is just a demo to use the nice system call.
 * used syscall: int nice(int inc);
 * nice() adds inc to the nice value for the calling process.  (A higher nice value means a low priority.)  Only the superuser may specify a negative increment, or pri-
 * ority increase.  The range for nice values is described in getpriority(2).
 */

/*
* RETURN VALUE
* On success, the new nice value is returned (but see NOTES below).  On error, -1 is returned, and errno is set appropriately.
* 
* ERRORS
* 	EPERM  The calling process attempted to increase its priority by supplying a negative inc but has insufficient privileges.  Under Linux the  CAP_SYS_NICE  capability
* is required.  (But see the discussion of the RLIMIT_NICE resource limit in setrlimit(2).)
* 
* 	NOTES
* 	SUSv2 and POSIX.1-2001 specify that nice() should return the new nice value.  However, the Linux syscall and the nice() library function provided in  older  versions
* 	of (g)libc (earlier than glibc 2.2.4) return 0 on success.  The new nice value can be found using getpriority(2).
* 
* 	Since glibc 2.2.4, nice() is implemented as a library function that calls getpriority(2) to obtain the new nice value to be returned to the caller.  With this imple-
* 	mentation, a successful call can legitimately return -1.  To reliably detect an error, set errno to 0 before the call, and check its value when nice() returns -1.
*/
	
int main(int argc, char * argv[])
{
	if(argc != 2)
	{
		printf("Usgae: %s <inc>\n", argv[0]);
		exit(1);
	}
	int inc = atoi(argv[1]);
	errno = 0;
	int ret = nice(inc);
	if(-1 == ret)
	{
		if(errno == EPERM)
		{
			printf("The calling process attempted to increase its priority by supplying a negative inc but has insufficient privileges.\n");
			exit(1);
		}
	}
	printf("success!\n");

	return 0;
}
